<?php

  /**
   * splits lines longer than 10 into several lines
   * (needed for secondlife e.g.)
   */

class Cicp_IOProcessor_ExtendLines implements Cicp_IOProcessor_Interface
{
    function parseInput($inputString) { }
    function parseOutput($outputString)
    {
        if (strpos($outputString, 'lin,') !== FALSE) {
            // there are lines, get graph
            $lines = explode("\n", $outputString);
            $lastLine = $lines[count($lines) - 1];
            $parameters = explode(',', $lastLine);
            $graphId = $parameters[7];
            unset($parameters);
            $graph = Cicp::getInstance()->getGraphManager()->getGraph($graphId);
            $nodes = $graph->getNodes();
            foreach ($nodes as $node) {
                if ($node->getNodeProperty('shape') == Cicp_Graph_Node::SHAPE_LINE) {
                    $nodeStart = $node->getNodeProperty('loc1');
                    $nodeEnd = $node->getNodeProperty('loc2');
                    $start = $nodeStart;
                    $distanceV = $nodeEnd->substract($nodeStart);
                    if ($distanceV->length() > 10) {
                        while ($distanceV->length() > 10) {
                            $partNode = clone $node;
                            $end = $distanceV->setLength(10);
                            $end = $start->add($end);
                            $partNode->setPosition($start->x,
                                                   $start->y,
                                                   $start->z);
                            $partNode->setDimensions($end->x,
                                                     $end->y,
                                                     $end->z);
                            $graph->registerNode($partNode);
                            $start = $end;
                            $distanceV = $nodeEnd->substract($end);
                        }
                        $node->setPosition($end->x,
                                           $end->y,
                                           $end->z);
                    }
                }
            }
            $outputString = $graph . '';
        }
        return $outputString;
    }
}